package com.ebelter.ty.ui.activity;


import android.content.Intent;
import android.util.TypedValue;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.ty.R;
import com.ebelter.ty.adapters.HostoryListAdapter;
import com.ebelter.ty.model.http.TyNetUtil;
import com.ebelter.ty.model.http.bean.HostoryListData;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class HistoryListActivity extends BaseActivity {

    private static final String TAG = "HistoryListActivity";

    ImageView back_iv;



    RelativeLayout no_history_ly;


    ListView history_lv;

    private HostoryListAdapter adapter;


    private int startPage;
    private int pageSize=10;

    private long lastLoadingTime;

    @Override
    protected int loadLayoutById() {
        return R.layout.historylist_ly;
    }


    @Override
    protected void initView() {

        back_iv = findViewById(R.id.modify_back_bt);
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        no_history_ly = findViewById(R.id.no_history_ly);
        history_lv = findViewById(R.id.history_lv);


        startPage=0;
        pageSize=10;
        //  StatusBarUtil.setDarkStatusIcon(this,false);
        history_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.i(TAG,"----获取到的数据是---"+adapter.getData().get(position));
                Intent deatilIntent=new Intent(HistoryListActivity.this,HistoryItemDetailActivity.class);
                deatilIntent.putExtra(HistoryItemDetailActivity.ITEMDATD,adapter.getData().get(position));
                startActivity(deatilIntent);
            }
        });
    }


    View listFootView;
    TextView noMoreTv;
    @Override
    protected void initData() {

        adapter=new HostoryListAdapter(this);
        history_lv.setAdapter(adapter);

        listFootView = View.inflate(this, R.layout.no_history, null);
        noMoreTv=listFootView.findViewById(R.id.no_data_tv);
        noMoreTv.setText(R.string.No_more);
        noMoreTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        history_lv.addFooterView(listFootView);
        ViewUtils.hideView(listFootView);

        getHostoryListDatas(startPage,pageSize);
        history_lv.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (System.currentTimeMillis()-lastLoadingTime>3000){
                            LogUtils.i(TAG,"---------加载更多");
                            lastLoadingTime=System.currentTimeMillis();
                            getHostoryListDatas(++startPage,pageSize);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

    }





    public void getHostoryListDatas(int startPage,int pageSize) {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        showLoadingDialog(StringUtils.getResStr(R.string.loading));
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("userId", UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put("startIndex", startPage);
        paramMap.put("pageSize", pageSize);
        TyNetUtil.getInstance().getsHostoryListData(this, paramMap, new HttpResponse<HostoryListData>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, HostoryListData hostoryListData,String s) {


                dissLoadingDialog();
                if (isRequestSuccess){
                    LogUtils.i(TAG,requestDes);
                    LogUtils.i(TAG,hostoryListData.toString());
                    if (hostoryListData.resultCode==0){
                        //2018-06-19 12:15:12
                        if (adapter!=null){
                            if (hostoryListData.getResultData()!=null&&hostoryListData.getResultData().getData()!=null){
                                adapter.addData(hostoryListData.getResultData().getData());
                                if (hostoryListData.getResultData().getData().size()==0){
                                    ViewUtils.displayView(listFootView);
                                }
                            }

                        }

                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }

                if (adapter!=null){
                    if (adapter.getCount()==0){
                        ViewUtils.displayView(no_history_ly);
                    }else {
                        ViewUtils.hideView(no_history_ly);
                    }
                }


            }
        });
    }
}
